package gui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.SpringLayout;

import utility.Record;

import business.GroupeBL;
import business.SemestreBL;

public class GroupeUIConsulter extends AbstractViewPane implements ActionListener {

	private JTextField fieldNom;
	private JSpinner spinNbEtud;
	private JList listeGroupes;
	private JList listeIncompatible;
	private JButton right;
	private JButton left;
	
	private String nom;
	private int nbEtud;
	private String[] nomGroupes = {};
	private String[] nomIncomp = {};
	private int groupeId;
	private int semestreId;
	
	ViewMode currentViewMode;
	
	public GroupeUIConsulter(ViewMode v, int groupe, int semId) {
		groupeId = groupe;
		semestreId = semId;
		currentViewMode = v;
		
		SemestreBL semestreBL = new SemestreBL(semId);
		
		if (currentViewMode == ViewMode.AJOUT) {
			ArrayList<Record> liste = semestreBL.getListeGroupe(); 
			nomGroupes = new String[liste.size()];
			for (int i = 0; i < liste.size(); i++)
				nomGroupes[i] = liste.get(i).getLibelle();
			nom = "";
			nbEtud = 0;
		}
		else {
			GroupeBL groupeBL = new GroupeBL(groupeId);
			nom = groupeBL.getNomGroupe();
			nbEtud = groupeBL.getNombreEtudiant();
			ArrayList<Record> listeIncomp = groupeBL.getListeGroupe(); 
			nomIncomp = new String[listeIncomp.size()];
			for (int i = 0; i < listeIncomp.size(); i++)
				nomIncomp[i] = listeIncomp.get(i).getLibelle();
			ArrayList<Record> liste = semestreBL.getListeGroupe(); 
			nomGroupes = new String[liste.size()];
			for (int i = 0; i < liste.size(); i++)
				nomGroupes[i] = liste.get(i).getLibelle();
		}
		
		
        String[] labels = {"Nom du groupe: ", "Nombre d'étudiants: ", "Groupes:", "Groupes incompatibles:"};
	
        //Create and populate the panel.
        JPanel p = new JPanel(new SpringLayout());

        JLabel l = new JLabel(labels[0], JLabel.TRAILING);
        p.add(l);
        
        fieldNom = new JTextField(nom);
        l.setLabelFor(fieldNom);
        p.add(fieldNom);

            if (currentViewMode == ViewMode.CONSULTATION) {
            	fieldNom.setEnabled(false);
            }

        l = new JLabel(labels[1], JLabel.TRAILING);
        p.add(l);

        SpinnerNumberModel sm = new SpinnerNumberModel(nbEtud, 0, 200, 1);
        spinNbEtud = new JSpinner(sm);
        l.setLabelFor(spinNbEtud);
        p.add(spinNbEtud);

            if (currentViewMode == ViewMode.CONSULTATION) {
            	spinNbEtud.setEnabled(false);
            }
            
        JLabel lGr = new JLabel(labels[2], JLabel.TRAILING);

        listeGroupes = new JList(nomGroupes);
        listeGroupes.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        //lGr.setLabelFor(listeGroupes);

        if (currentViewMode == ViewMode.CONSULTATION) {
            	listeGroupes.setEnabled(false);
        }
            
        JLabel lInc = new JLabel(labels[3], JLabel.TRAILING);

        listeIncompatible = new JList(nomIncomp);
        listeIncompatible.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        //lInc.setLabelFor(listeIncompatible);

            if (currentViewMode == ViewMode.CONSULTATION) {
            	listeIncompatible.setEnabled(false);
            }

        left = new JButton("<");
        right = new JButton(">");
        
        if (currentViewMode == ViewMode.CONSULTATION) {
        	left.setEnabled(false);
        	right.setEnabled(false);
    }

        
		right.setActionCommand("right");
		left.setActionCommand("left");
        
        right.addActionListener(this);
        left.addActionListener(this);

        p.add(lGr);
        p.add(lInc);
        p.add(listeGroupes);
        p.add(listeIncompatible);

        p.add(left);
        p.add(right);
        
                
        //Lay out the panel.
        SpringUtilities.makeCompactGrid(p,
                                        5, 2, //rows, cols
                                        6, 6,        //initX, initY
                                        6, 6);       //xPad, yPad
        
        add(p, BorderLayout.NORTH);
	    
        if (currentViewMode != ViewMode.CONSULTATION) {
        	// Mode modification - afficher les boutons
	        JPanel commandBox = new JPanel();
	        commandBox.add(new JButton("Enregistrer"));
	        drawCommandBox(commandBox);
        }
	
	}
	@Override
	public String getTitle() {
		// TODO Auto-generated method stub
		if (currentViewMode == ViewMode.CONSULTATION)
			return "Consultation d'un groupe";
		else if (currentViewMode == ViewMode.AJOUT)
			return "Ajout d'un groupe";
		else
			return "Modification d'un groupe";
	}

	@Override
	public boolean hasBeenModified() {
		// TODO Auto-generated method stub
		return (currentViewMode != ViewMode.CONSULTATION);
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand().equals("right")) {
			if (listeGroupes.getSelectedIndex() != -1) {
				System.out.println("right");	
			}
		}
		if (e.getActionCommand().equals("left")) {
			if (listeIncompatible.getSelectedIndex() != -1) {
				System.out.println("left");
			}
		}
	}

}
